package cn.edu.hhu.servlet.student;

import cn.edu.hhu.Bean.AttendanceInfo;
import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.DAO.impl.AttendanceTaskDaoImpl;
import cn.edu.hhu.service.Impl.StudentServiceImpl;
import cn.edu.hhu.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Currency;

/**
 * @anthor: lddd
 * @date: 2020/5/11 - 14:51
 * @description:
 **/
@WebServlet("/student/checkSignCodeServlet")
public class CheckSignCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String task_num = request.getParameter("task_num");
        String signin_pwd = request.getParameter("signin_pwd");

        //查询当前任务
        AttendanceTask submit_Task = new AttendanceTask();
        submit_Task.setTnum(Long.parseLong(task_num));
        AttendanceTaskDaoImpl TaskDao = new AttendanceTaskDaoImpl();
        AttendanceTask sql_Task = TaskDao.queryTaskByTnum(submit_Task.getTnum());

        //检查是否超过截止时间
        Timestamp now = new Timestamp(System.currentTimeMillis());
        Timestamp end_time = sql_Task.getEnd_time();
        if (now.compareTo(end_time) < 0) {
            //没有超过截止时间
            StudentService studentService = new StudentServiceImpl();
            Student student = (Student) request.getSession().getAttribute("loginStudent");
            AttendanceTask attendanceTask = new AttendanceTask();
            attendanceTask.setTnum(Long.parseLong(task_num));
            attendanceTask.setPwd(signin_pwd);
            //签到
            boolean signin = studentService.signin(student, attendanceTask);
            request.setAttribute("attendanceTask", sql_Task);
            if (signin) {
                //签到成功
                Student loginStudent = (Student) request.getSession().getAttribute("loginStudent");//登录的学生用户
                AttendanceInfo sql_Info = studentService.queryAttendanceInfo(loginStudent, submit_Task);
                request.setAttribute("attendanceInfo", sql_Info);
                request.getRequestDispatcher("/student/signinResult/success.jsp").forward(request,response);
            } else {
                //签到失败，签到码错误
                request.getRequestDispatcher("/student/signinResult/wrongSigninCode.jsp").forward(request,response);
            }
        } else {
            //活动已过期
            request.getRequestDispatcher("/student/signinResult/over.jsp").forward(request,response);
            System.out.println("活动已过期！");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
